#!/bin/bash

########################## test 与 [] #####################3
# $1 $2
#
# if [ $1 -gt $2 ]; then
#     echo "$1 is greater than $2"
# elif [ $1 -lt $2 ]; then
#     echo "$2 is greater than $1"
# else
#     echo "Both are equal"
# fi


##########################  [[]] ###########################
# # ## 1. 通配符
# if [[ "$1" == *.log ]]; then
#     echo "The file is a log file"
# else
#     echo "The file is not a log file"
# fi

## 2. 正则表达式
## \. 表示转义,即匹配点字符
## 正则表达式规则:
## ^ 匹配字符串开头
## $ 匹配字符串结尾
## . 匹配任意字符
## * 匹配零个或多个字符
## + 匹配一个或多个字符
## ? 匹配零个或一个字符

## {n} 匹配n个字符
## {n,} 匹配n个或更多字符
## {n,m} 匹配n到m个字符

## [] 匹配括号中的任意字符
## | 匹配或
## (...) 匹配括号中的表达式

# if [[ "$1" =~ \.log ]]; then
#     echo "The file matches the log file pattern"
# else
#     echo "The file does not match the log file pattern"
# fi



# 验证IP地址是否有效 
# bug:999.999.999.999 也会被认为是有效的IP地址
# if [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
#     echo "The IP address is valid"
# else
#     echo "The IP address is invalid"
# fi

# 优化:
# [1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]
# 这个正则表达式可以匹配IP地址的各个部分,并且保证每部分的取值范围在0-255之间
# (^([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

# 2层中括号的写法:
# if [[ "$1" =~ ^([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]; then
#     echo "The IP address is valid"
# else
#     echo "The IP address is invalid"
# fi
